<template>
  <div class="race-page">
    <div class="header">
      <div class="round-info">
        <span class="round-text">第 {{ this.roundIndex+1 }} 轮</span>
        <span class="timer">剩余时间: {{ minutes }}:{{ seconds }}</span>
      </div>
      <div class="btn">
        <el-button type="primary" @click="nextRound" icon="el-icon-arrow-right">
          下一轮
        </el-button>
        <!-- <el-button type="primary" @click="dialogVisible = true">确定</el-button> -->
      </div>
    </div>

    <el-divider />

    <!-- 管理员界面 -->
    <el-card v-if="role === 'sa' || role === 'teacher' || role === 'student'" class="admin-panel">
      <div v-if="winner" class="winner-info">
        <el-alert
          title="抢答成功！"
          type="success"
          :description="`抢答者：${winner.name}`"
        />
        <div v-if="submittedAnswer" class="submitted-answer">
          <h3>考生提交的信息：</h3>
          <p>选中人员：{{ submittedAnswer.selectedPerson }}</p>
          <p>姓名：{{ submittedAnswer.name }}</p>
          <p>所住小区：{{ submittedAnswer.community }}</p>
        </div>
      </div>

      <!-- 显示辖区人员头像 -->
      <div class="people-grid">
        <div
          v-for="person in popQuList"
          :key="person.id"
          class="person-card"
          :class="{ 
            'selected': selectPopQu.includes(person),
            'disabled': selectedHistory.includes(person.id)
          }"
          @click="selectPerson(person)"
        >
          <div class="avatar-container">
            <img :src="person.image" alt="" class="avatar" />
          </div>
          <div class="serial-number">
            <el-tag size="medium" type="info">编号：{{ person.id }}</el-tag>
          </div>
        </div>
      </div>
      
    </el-card>
    <div class="next-round">
        <!-- <el-button type="primary" @click="fetchData('123')" icon="el-icon-arrow-right">
          下一轮
        </el-button> -->
        <el-button type="primary" @click="dialogVisible = true">确定</el-button>
        
      </div>
    <el-dialog
      :visible.sync="dialogVisible"
      width="100%"
      :before-close="handleClose"
      :fullscreen="true"
      :center="true"
    >
      <el-row :gutter="20" type="flex" justify="center" align="middle">
        <el-col :span="12" v-for="item in selectPopQu" :key="item.id">
          <el-card class="comparison-card">
            <div class="image-container">
              <img :src="item.image" :alt="item.name">
            </div>
            <div class="answer-container" v-if="showAnswer">
              <h2>社区：{{ item.address }}</h2>
              <h3>姓名： {{ item.name }}</h3>
              <p>管控类型： {{ item.content }}</p>
              <p>当前去向：{{ item.job }}</p>
            </div>
            <div class="dialog-serial-number">
              <el-tag size="medium" effect="dark" type="warning">编号：{{ item.id }}</el-tag>
            </div>
            <div class="questions-container">
              <div 
                v-for="(question, index) in questions" 
                :key="index"
                class="question-item"
                @click="toggleQuestionAnswer(index)"
              >
                <div class="question-header">
                  <h3>问题 {{ index + 1 }}</h3>
                  <el-button type="text" @click.stop="toggleQuestionAnswer(index)">
                    {{ questionShowAnswer[index] ? '隐藏答案' : '显示答案' }}
                  </el-button>
                </div>
                <div class="question-content">
                  <p class="question-text">{{ question.text }}</p>
                  <div v-if="questionShowAnswer[index]" class="answer-text">
                    <p>{{ question.answer }}</p>
                  </div>
                </div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
      <div class="dialog-footer">
        <el-button type="primary" @click="toggleAnswer">
          {{ showAnswer ? '隐藏信息' : '显示信息' }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { saveRob, saveQuick } from "@/api/exam/exam";
import { fetchQucikDetail } from "@/api/paper/exam";

export default {
  name: "Population",

  data() {
    return {
      role: "student",
      winner: null,
      submittedAnswer: null,
      popQuList: [],
      selectPopQu: [],
      dialogVisible: false,
      showAnswer: false,
      selectedHistory: [],  
      roundArr:['important'],
      timer: null,
      timeLeft: 60, // 1分钟 = 60秒
      minutes: '00',
      seconds: '60',
      roundIndex: 0,
      questions: [
        {
          text: "请分析该人员的管控类型和原因",
          answer: "该人员属于重点关注对象，原因是..."
        },
        {
          text: "针对该情况，应采取哪些管控措施？",
          answer: "1. 定期走访\n2. 及时登记信息\n3. 建立管控档案"
        }
      ],
      questionShowAnswer: {},
    };
  },

  created() {
    this.role = this.$store.state.user.roles[0];
    this.fetchData();
    this.startTimer();
  },

  methods: {
    startTimer() {
      this.timeLeft = 60
      this.minutes = 0
      this.seconds = 60
      if(this.timer) {
        clearInterval(this.timer)
      }
      
      this.timer = setInterval(() => {
        this.timeLeft--
        this.minutes = Math.floor(this.timeLeft / 60).toString().padStart(2, '0')
        this.seconds = (this.timeLeft % 60).toString().padStart(2, '0')
        
        if(this.timeLeft <= 0) {
          this.$message.warning('本轮时间已到!')
          clearInterval(this.timer)
        }
      }, 1000)
    },

    nextRound() {
      if(this.roundIndex < this.roundArr.length) {
        this.fetchData();
        this.startTimer();
        this.roundIndex++;
      } else {
        this.$message.warning('已经是最后一轮了');
      }
    },

   
    fetchData() {
      const params = {id:this.roundArr[0]}
      fetchQucikDetail(params).then(res => {
        // console.log(res);
        this.popQuList = res.data.popQu;
      });
    },

    selectPerson(person) {
      this.selectPopQu.push(person);
      if (this.selectPopQu.length > 2) {
        this.selectPopQu.shift();
      }
    },

    handleClose() {
      this.selectPopQu = [];
      this.dialogVisible = false;
      this.showAnswer = false;
    },

    toggleAnswer() {
      this.showAnswer = !this.showAnswer;
    },

    toggleQuestionAnswer(index) {
      this.$set(this.questionShowAnswer, index, !this.questionShowAnswer[index]);
    }
  },

  beforeDestroy() {
    if(this.timer) {
      clearInterval(this.timer)
    }
  }
};
</script>

<style lang="scss" scoped>
.race-page {
  padding: 20px;
  background-color: #f9f9f9;
  min-height: 100vh;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  /* margin-bottom: 20px; */
}

.round-info {
  display: flex;
  align-items: center;
  gap: 20px;
}

.round-text {
  font-size: 24px;
  font-weight: bold;
  color: #409EFF;
}

.timer {
  font-size: 24px;
  color: #f56c6c;
  font-weight: bold;
}

.el-divider{
  margin: 1px;
}

.btn {
  display: flex;
  justify-content: flex-end;
}

.admin-panel {
  margin: 20px auto;
  max-width: 1200px;
  /* padding: 20px; */
  background-color: #ffffff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border-radius: 8px;
}

.people-grid {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 30px;
  margin: 10px 0;
}

.person-card {
  text-align: center;
  padding: 10px;
  border: 1px solid #eee;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.person-card.selected {
  border-color: #409eff;
  box-shadow: 0 0 10px rgba(64, 158, 255, 0.5);
}

.person-card.disabled {
  opacity: 0.5;
  cursor: not-allowed;
  background-color: #f5f5f5;
}

.avatar-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  margin-bottom: 10px;
}

.avatar {
  width: 100%;
  height: 100%;
  /* border-radius: 50%; */
  object-fit: cover;
}

.serial-number {
  margin-top: 8px;
  display: flex;
  justify-content: center;
}

.next-round {
  display: flex;
  justify-content: center;
  margin-top: 10px;
}

.winner-info {
  margin-top: 20px;
}

.submitted-answer {
  margin-top: 20px;
  padding: 10px;
  background-color: #f0f9eb;
  border: 1px solid #e1f3d8;
  border-radius: 4px;
}

.questions-container {
  margin-top: 30px;
  padding: 20px;
  text-align: left;

  .question-item {
    background-color: #f8f9fa;
    border-radius: 8px;
    padding: 20px;
    margin-bottom: 20px;
    cursor: pointer;
    transition: all 0.3s ease;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    }

    .question-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 15px;

      h3 {
        font-size: 24px;
        color: #303133;
        margin: 0;
      }
    }

    .question-content {
      .question-text {
        font-size: 20px;
        color: #606266;
        margin-bottom: 15px;
        line-height: 1.6;
      }

      .answer-text {
        background-color: #ecf5ff;
        padding: 15px;
        border-radius: 6px;
        margin-top: 10px;

        p {
          font-size: 18px;
          color: #409EFF;
          line-height: 1.8;
          margin: 0;
          white-space: pre-line;
        }
      }
    }
  }
}

.comparison-card {
  margin: 20px;
  text-align: center;
  padding: 20px;
  
  .image-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 400px;
    margin: 20px 0;
    
    img {
      max-width: 100%;
      max-height: 100%;
      object-fit: contain;
    }
  }
  
  .answer-container {
    margin: 20px 0;
    padding: 15px;
    background-color: #f5f7fa;
    border-radius: 4px;
    
    h3 {
      color: #409EFF;
      margin-bottom: 10px;
    }
    
    p {
      color: #606266;
      line-height: 1.6;
      margin: 5px 0;
    }
  }
}

.dialog-footer {
  text-align: center;
  margin-top: 20px;
}

.dialog-serial-number {
  margin-top: 15px;
}
</style>